package com.crois.barrier.admin.listener;


import com.alibaba.fastjson.JSON;
import com.crois.barrier.admin.entity.LogSend;
import com.crois.barrier.admin.entity.StatisticsSend;
import com.crois.barrier.admin.service.ILogSendSvc;
import com.crois.barrier.admin.service.IStatisticsSendSvc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.rabbitmq.client.Channel;
import java.io.IOException;

@Slf4j
@Component
public class MqListener {

    @Autowired
    @Qualifier("logSendSvc")
    private ILogSendSvc logSendSvc;

    @Autowired
    @Qualifier("statisticsSendSvc")
    private IStatisticsSendSvc statisticsSendSvc;


    /*****
     *
     * @param channel
     * @param message
     * @throws IOException
     */
    @Transactional
    @RabbitHandler
    @RabbitListener(bindings = {@QueueBinding(value =
    @Queue(value = "barrier.statistics.send.queue",durable = "true",autoDelete = "false"),
            exchange = @Exchange(type = ExchangeTypes.DIRECT,value = "barrier.statistics.exchange")
            ,key = "statistics.send")})
    public void statisticsSendListener(Channel channel, Message message) throws IOException {
        try {
            log.info("接受到的消息为"+new String(message.getBody(),"UTF-8"));
            MessageProperties messageProperties = message.getMessageProperties();
            String jsonString = new String(message.getBody(), "UTF-8");
            StatisticsSend statisticsSend = JSON.parseObject(jsonString, StatisticsSend.class);
            statisticsSendSvc.addStatisticsSend(statisticsSend);
            // 确认收到消息，false只确认当前consumer一个消息收到，true确认所有consumer获得的消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("-----------"+messageProperties.getMessageId()+"消息处理成功！");
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                log.info("消息已重复处理失败,拒绝再次接收" );
                // 拒绝消息，requeue=false 表示不再重新入队，如果配置了死信队列则进入死信队列
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                log.info("消息即将再次返回队列处理" );
                // requeue为是否重新回到队列，true重新入队
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
            //e.printStackTrace();
        }
    }


    /*****
     *
     * @param channel
     * @param message
     * @throws IOException
     */
    @Transactional
    @RabbitHandler
    @RabbitListener(bindings = {@QueueBinding(value =
    @Queue(value = "barrier.log.send.queue",durable = "true",autoDelete = "false"),
            exchange = @Exchange(type = ExchangeTypes.DIRECT,value = "barrier.log.exchange")
            ,key = "log.send")})
    public void logSendListener(Channel channel, Message message) throws IOException {
        try {
            log.info("接受到的消息为"+new String(message.getBody(),"UTF-8"));
            MessageProperties messageProperties = message.getMessageProperties();
            String jsonString = new String(message.getBody(), "UTF-8");
            LogSend logSend = JSON.parseObject(jsonString, LogSend.class);
            logSendSvc.addLogSend(logSend);
            // 确认收到消息，false只确认当前consumer一个消息收到，true确认所有consumer获得的消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("-----------"+messageProperties.getMessageId()+"消息处理成功！");
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                log.info("消息已重复处理失败,拒绝再次接收" );
                // 拒绝消息，requeue=false 表示不再重新入队，如果配置了死信队列则进入死信队列
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                log.info("消息即将再次返回队列处理" );
                // requeue为是否重新回到队列，true重新入队
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
            //e.printStackTrace();
        }
    }

}
